package com.qiantu.phone.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qiantu.api.entity.FloorBean;
import com.qiantu.api.entity.RoomBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppAdapter;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 *    author : Android 轮子哥
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2019/09/22
 *    desc   : 房间标签数据列表
 */
public class RoomFloorDialogAdapter extends AppAdapter<FloorBean> {
    private boolean hasAllItem = false;//最前面是否带有所有选项
    private Map<String, RoomBean> mSelected = new LinkedHashMap<>();
    private Map<String, String> mSelectAreaTitle = new LinkedHashMap<>();
    private boolean canBeEmpty = false;
    public RoomFloorDialogAdapter(Context context) {
        super(context);
    }

    public Map<String, RoomBean> getSelectMap(){
        return mSelected;
    }

    public void setSelectMap(Map<String, RoomBean> data){
        mSelected = data;
        notifyDataSetChanged();
    }

    public Map<String, String> getSelectAreaTitle(){
        return mSelectAreaTitle;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder();
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {

        private final TextView tvFloorName;
        private final RecyclerView roomRecyclerView;
        private final RoomBeanLabelAdapter roomLabelAdapter;

        private ViewHolder() {
            super(R.layout.room_floor_dialog_item);
            tvFloorName = findViewById(R.id.tv_floor_name);
            roomRecyclerView = findViewById(R.id.room_recycler_view);
            roomRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 4));
            roomLabelAdapter = new RoomBeanLabelAdapter(getContext()){
                @Override
                public void onSelectChanged(Map<String, RoomBean> selectBean) {
                    RoomFloorDialogAdapter.this.onSelectChanged(mSelected);
                }
            };
            roomRecyclerView.setAdapter(roomLabelAdapter);
        }

        @Override
        public void onBindView(int position) {
            FloorBean floor = getItem(position);
            //默认楼层不显示
            if(floor.getType() == 1 || TextUtils.isEmpty(floor.getFloorName())){
                tvFloorName.setVisibility(View.GONE);
            }else{
                tvFloorName.setVisibility(View.VISIBLE);
                tvFloorName.setText(floor.getFloorName());
            }
            if (hasAllItem && floor.getType() != 1){
                roomLabelAdapter.setCanBeEmpty(true);
                roomLabelAdapter.setDataWithHeadAllItem(floor, mSelected, mSelectAreaTitle);
            }else {
                roomLabelAdapter.setSelected(mSelected);
                roomLabelAdapter.setData(floor.getRooms());
                roomLabelAdapter.setCanBeEmpty(canBeEmpty);
            }
        }
    }

    public void clearSelected(){
        mSelected.clear();
        mSelectAreaTitle.clear();
        notifyDataSetChanged();
    }

    public void setCanBeEmpty(boolean value){
        canBeEmpty = value;
        notifyDataSetChanged();
    }

    /**
     * 设置楼层房间前面时候有全选按键
     * @param value
     */
    public void setHasAllItem(boolean value){
        hasAllItem = value;
    }

    //外部实现
    public void onSelectChanged(Map<String, RoomBean> selectBean){}

    public static class Title{
        public String title;
    }
}